<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="用户名" class="myform-item">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="岗位" class="myform-item">
          <el-input
            v-model="form.position_name"
            placeholder="请输入岗位"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-button type="primary" @click="onUpdateBatch" style="margin-bottom: 10px"
      >批量修改身份</el-button
    >
    <el-table
      :data="tableData"
      style="width: 100%"
      ref="multipleTable"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        label="用户名"
        align="center"
        prop="username"
      ></el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <div :style="{ color: row.status == 1 ? '#2c70d6' : 'red' }">
            {{ row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="部门"
        align="center"
        prop="department_name"
      ></el-table-column>
      <el-table-column
        label="岗位"
        align="center"
        prop="position_name"
      ></el-table-column>
      <el-table-column
        label="后台身份"
        align="center"
        prop="admin_level_name"
      ></el-table-column>
      <el-table-column
        label="小程序身份"
        align="center"
        prop="home_level_name"
      ></el-table-column>
      <el-table-column
        label="客户端身份"
        align="center"
        prop="pc_level_name"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <!-- <el-button type="text" @click="onLook(row.id)">查看</el-button> -->
          <el-button type="text" :disabled="!qxobj.save" @click="onEdit(row.id)"
            >编辑</el-button
          >
          <el-button
            type="text"
            :disabled="!qxobj.save"
            @click="onOff(row.id, 0, row.username)"
            v-if="row.status == 1"
            style="color: red"
            >禁用</el-button
          >
          <el-button
            type="text"
            :disabled="!qxobj.save"
            @click="onOff(row.id, 1, row.username)"
            v-else
            >启用</el-button
          >
          <!-- <el-button type="text" @click="onDelete(row.id)" style="color: red"
            >删除</el-button
          >-->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminUserUserlist"
    />
    <el-dialog
      title="批量修改身份"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="后台" name="0">
          <el-select
            v-model="upDateForm.admin_level"
            placeholder="请选择用户身份"
          >
            <el-option
              v-for="item in tusersrolelist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-tab-pane>
        <el-tab-pane label="小程序" name="1">
          <el-select
            v-model="upDateForm.home_level"
            placeholder="请选择用户身份"
          >
            <el-option
              v-for="item in tusersrolelist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-tab-pane>
        <el-tab-pane label="客户端" name="2">
          <el-select v-model="upDateForm.pc_level" placeholder="请选择用户身份">
            <el-option
              v-for="item in tusersrolelist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-tab-pane>
        <el-tab-pane label="仓库客户端" name="5">
          <el-select v-model="upDateForm.cang_level" placeholder="请选择用户身份">
            <el-option
              v-for="item in tusersrolelist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :loading="loading"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminUserUserlist,
  postAdminUserDeltrole,
  postAdminUserGetusersrolelist,
  postAdminUserSetpilianguser,
  postAdminUserSetUserStatus,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      dialogVisible: false,
      qxobj: {},
      loading: false,
      form: {
        position_name: "",
        username: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
      options: [
        {
          label: "后台",
          value: "0",
        },
        {
          label: "小程序",
          value: "1",
        },
        {
          label: "客户端",
          value: "2",
        },
        {
          label: "仓库客户端",
          value: "5"
        }
      ],
      optionsTwo: [
        {
          label: "启用",
          value: "1",
        },
        {
          label: "禁用",
          value: "0",
        },
      ],
      upDateForm: {
        ids: [],
        home_level: "",
        pc_level: "",
        admin_level: "",
        cang_level:""
      },
      multipleSelection: [],
      tusersrolelist: [],
      activeName: 0,
    };
  },
  activated() {
    this.qxobj = JSON.parse(this.$route.query.qxobj);
    let form = this.form;
    this.postAdminUserUserlist(form);
  },
  methods: {
    onOff(id, status, username) {
      let that = this;
      let obj = {
        id,
        status,
      };
      this.$confirm(
        `确定${status == 1 ? "启用" : "禁用"}${username}？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          postAdminUserSetUserStatus(obj).then((res) => {
            let form = that.form;
            that.postAdminUserUserlist(form);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    onSubmit() {
      let { multipleSelection, upDateForm, form } = this;
      this.loading = true;
      if (
        upDateForm.home_level ||
        upDateForm.pc_level ||
        upDateForm.admin_level
      ) {
        multipleSelection.forEach((res) => {
          upDateForm.ids.push(res.id);
        });
        postAdminUserSetpilianguser(upDateForm).then((res) => {
          this.loading = false;
          this.dialogVisible = false;
          this.postAdminUserUserlist(form);
        });
      } else {
        this.loading = false;
        this.$message.warning("请至少选择一个身份");
      }
    },
    handleSelectionChange(val) {
      console.log("val--------->", val);
      this.multipleSelection = val;
    },
    // 标签改变时
    handleClick(e) {
      this.postAdminUserGetusersrolelist(this.activeName);
    },
    // 批量修改身份
    onUpdateBatch() {
      if (this.multipleSelection.length) {
        this.upDateForm = {
          ids: [],
          home_level: "",
          pc_level: "",
          admin_level: "",
        };
        this.activeName = 0;
        this.postAdminUserGetusersrolelist(this.activeName);
        this.dialogVisible = true;
      } else {
        this.$message.warning("请选择需要批量修改的用户~~~");
      }
    },
    // 权限身份列表
    async postAdminUserGetusersrolelist(type) {
      let obj = {
        type,
      };
      await postAdminUserGetusersrolelist(obj).then((res) => {
        console.log("权限身份列表------------>", res);
        let obj = {
          id: "",
          title: "无",
        };
        let data = res.data;
        data.unshift(obj);
        this.tusersrolelist = data;
      });
    },
    // 删除
    onDelete(id) {
      let obj = {
        id,
      };
      this.$confirm("确定删除该模块?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminUserDeltrole(obj).then((res) => {
            let form = this.form;
            this.postAdminUserUserlist(form);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    onEdit(id) {
      this.$router.push({ name: "UserAdminUserDetail", query: { id } });
    },
    // qc列表
    async postAdminUserUserlist(data) {
      this.loading = true;
      this.form = data;
      let form = JSON.parse(JSON.stringify(this.form));
      this.$refs.multipleTable.clearSelection();
      // if (form.qc_time) {
      //   form.qc_time = `${form.qc_time[0]}/${form.qc_time[1]}`;
      // }
      postAdminUserUserlist(form)
        .then((res) => {
          this.loading = false;
          console.log("qc列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminUserUserlist(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
